const Base = require('./base.js');
const moment = require('moment');
module.exports = class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        const page = this.get('page') || 1;
        const size = this.get('size') || 10;
        const sortField = this.get('sortfield') || 'id';
        const shelves_beg = this.get('begin') || '';
        const shelves_end = this.get('end') || '';
        const name = this.get('name') || '';
        let seachdata = { }, timesearch = {};
        if (name) {
            seachdata.name = ['like', `%${name}%`];
        }

        if (shelves_beg || shelves_end) {
            if (shelves_beg) {
            //   timesearch['egt'] = shelves_beg;
              seachdata.begindate = ['egt', shelves_beg];
            }
            if (shelves_end) {
            //   timesearch['elt'] = shelves_end;
              seachdata.enddate = ['elt', shelves_end];
            }
        }

        const model = this.model('active_site');
        const data = await model.where(
            seachdata
        ).order([sortField]).page(page, size).countSelect();
        for(const item of data.data){
            item.joinnum= await this.model('active_site_user').where({active_site_id:item.id}).count('distinct user_id');
        }
        return this.success(data);
    }

    async saveAction() {
        const model = this.model('active_site');
        let values = this.post();
        let id = this.post('id')||0;
        if (id>0) {
            id=await model.where({ id: id }).update(values);
        } else {
            values['id']=0;
            values['add_time']=Math.floor(new Date().getTime()/1000);
            id = await model.add(values);
            values['id'] = id;
        }
        return this.success(values);
    }
    async updateAction() {
        const model = this.model('active_site');
        const values = this.post();
        values['update_time']=Math.floor(new Date().getTime()/1000);
        const id = this.post('id');
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }

    async readAction() {
        const model = this.model('active_site');
        const id = this.get('id');
        const data=await model.where({id: id}).find();
        return this.success(data);
    }

    async deleteAction() {
        const model = this.model('active_site');
        const id = this.post('id');
        let data = await model.where({ id: id }).delete();
        return this.success(data);
    }
     /**
     * 任务激活
     */
    async active_siteAction() {
        const model = this.model('active_site');
        const values = this.post();
        const id = this.post('id');
        values['is_delete'] = 0;
        values['update_time']=Math.floor(new Date().getTime()/1000);
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }
    /**
     * 任务冻结
     */
    async freezeAction() {
        const model = this.model('active_site');
        const id = this.post('id');
        let data = await model.where({ id: id }).update({is_delete:1,update_time:Math.floor(new Date().getTime()/1000)});
        return this.success(data);
    }

     /**
     * 获取已激活任务
     */
    async getActivesiteAction() {
        const model = this.model('active_site');
        let data = await model.where({ is_delete: 0 }).select();
        return this.success(data);
    }

    //审核
    async verifyAction() {
        const model = this.model('active_site_user');
        let id=this.post('id')||0;
        let data = await model.where({id:id}).update({ is_verify: 1, verify_time:Math.floor(new Date().getTime()/1000)});
        return this.success(data);
    }
    //查看
    async lookJoinAction() {
        const model = this.model('active_site_user');
        let id=this.post('id')||0;
        let data = await model
        // .join({ table: 'user',  join: 'left', as:'u', on: ['user_id', 'id'] })
        .where({ 'id':id }).find();
        return this.success(data);
    }


    async joinListAction() {//参与列表
        const model = this.model('active_site_user');
        const page = this.get('page') || 1;
        const size = this.get('size') || 20;
        const id=this.get('id') || 0;
        const name=this.get('name') || '';
        const nickname=this.get('nickname') || '';
        const mobile=this.get('mobile') || '';
        let searchData={};
        if(id){
            searchData.active_site_id=id;
        }
        if(name){
            searchData['u.name']=['like',`%${name}%`];
        }
        if(mobile){
            searchData['u.mobile']=['like',`%${mobile}%`];
        }
        if(nickname){
            nickname = buffer.toString('base64');
            searchData['u.nickname']=['like', `%${nickname}%`];
        }
    const datas = await model.join({ table: 'user',  join: 'left', as:'u', on: ['user_id', 'id'] })
    .where( searchData).field("u.id as uid, u.name, u.gender,u.mobile, u.nickname,u.province,u.city,hiolabs_active_site_user.*").page(page, size).countSelect();
    for(const item of datas.data){
        item.nickname = Buffer.from(item.nickname, 'base64').toString();
        item.verifyText=item.is_verify?'是':'否';
        item.interTime=moment.unix(item.add_time).format('YYYY-MM-DD HH:mm:ss');
        if(item.verify_time)    item.verifyTime=moment.unix(item.verify_time).format('YYYY-MM-DD HH:mm:ss');
        
    }

        return this.success(datas);
    }

      

};